﻿namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
{
    using System;
    using System.Collections.Generic;
    using System.Data.Linq;
    using System.Linq;
    using System.Reflection;
    using System.Xml.Linq;

    internal static class TypeUtilities
    {
        private static readonly Type[] predefinedGenericTypes = new Type[] { typeof(IEnumerable<>), typeof(Nullable<>) };
        private static readonly Type[] predefinedTypes = new Type[] { typeof(string), typeof(decimal), typeof(DateTime), typeof(DateTimeOffset), typeof(TimeSpan), typeof(Guid), typeof(XElement) };

        public static Type FindIEnumerable(Type seqType)
        {
            if ((seqType != null) && (seqType != typeof(string)))
            {
                if (seqType.IsArray)
                {
                    return typeof(IEnumerable<>).MakeGenericType(new Type[] { seqType.GetElementType() });
                }
                if (seqType.IsGenericType)
                {
                    foreach (Type type in seqType.GetGenericArguments())
                    {
                        Type type2 = typeof(IEnumerable<>).MakeGenericType(new Type[] { type });
                        if (type2.IsAssignableFrom(seqType))
                        {
                            return type2;
                        }
                    }
                }
                Type[] interfaces = seqType.GetInterfaces();
                if ((interfaces != null) && (interfaces.Length > 0))
                {
                    foreach (Type type3 in interfaces)
                    {
                        Type type4 = FindIEnumerable(type3);
                        if (type4 != null)
                        {
                            return type4;
                        }
                    }
                }
                if ((seqType.BaseType != null) && (seqType.BaseType != typeof(object)))
                {
                    return FindIEnumerable(seqType.BaseType);
                }
            }
            return null;
        }

        internal static Type GetAssociatedMetadataType(Type type)
        {
            Type type2 = null;
            Attribute attribute = (from a in type.GetCustomAttributes(true).OfType<Attribute>()
                where a.GetType().Name == BusinessLogicClassConstants.MetadataTypeAttributeTypeName
                select a).FirstOrDefault<Attribute>();
            if (attribute != null)
            {
                PropertyInfo property = attribute.GetType().GetProperty("MetadataClassType");
                if (property != null)
                {
                    type2 = property.GetValue(attribute, null) as Type;
                }
            }
            return type2;
        }

        public static Type GetElementType(Type type)
        {
            if (!IsPredefinedSimpleType(type))
            {
                if (type.HasElementType)
                {
                    return GetElementType(type.GetElementType());
                }
                if (IsPredefinedGenericType(type))
                {
                    return GetElementType(type.GetGenericArguments()[0]);
                }
                Type type2 = FindIEnumerable(type);
                if (type2 != null)
                {
                    Type type3 = type2.GetGenericArguments()[0];
                    return GetElementType(type3);
                }
            }
            return type;
        }

        public static bool IsPredefinedGenericType(Type type)
        {
            if (type.IsGenericType)
            {
                type = type.GetGenericTypeDefinition();
            }
            else
            {
                return false;
            }
            foreach (Type type2 in predefinedGenericTypes)
            {
                if (type2 == type)
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsPredefinedSimpleType(Type type)
        {
            if (type.IsPrimitive)
            {
                return true;
            }
            if (type.IsEnum)
            {
                return true;
            }
            foreach (Type type2 in predefinedTypes)
            {
                if (type2 == type)
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsPredefinedType(Type type)
        {
            if ((!IsPredefinedSimpleType(type) && !IsPredefinedGenericType(type)) && !(type == typeof(byte[])))
            {
                return (type == typeof(Binary));
            }
            return true;
        }
    }
}

